Imports System.Data.SqlClient

Public Class cls_config_parametrousuario_

    Implements ICloneable

#Region "Variables ..."
    Private int_idparametrousuario As Integer = 0
    Private str_idparametro As String = ""
    Private str_nombre As String = ""
    Private int_idusuario As Integer = 0
    Private str_valor As String = ""

    Private obj_config_parametro As cls_config_parametro_

    Private col_Items As Collections.Generic.List(Of cls_config_parametrousuario_) = Nothing
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idparametrousuario
        End Get
    End Property
    Property idparametrousuario() As Integer
        Get
            idparametrousuario = int_idparametrousuario
        End Get
        Set(ByVal value As Integer)
            int_idparametrousuario = value
        End Set
    End Property
    Property idparametro() As String
        Get
            idparametro = str_idparametro
        End Get
        Set(ByVal value As String)
            str_idparametro = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property idusuario() As Integer
        Get
            idusuario = int_idusuario
        End Get
        Set(ByVal value As Integer)
            int_idusuario = value
        End Set
    End Property
    Property valor() As String
        Get
            valor = str_valor
        End Get
        Set(ByVal value As String)
            str_valor = value
        End Set
    End Property

    Property config_parametro() As cls_config_parametro_
        Get
            If IsNothing(obj_config_parametro) Then
                obj_config_parametro = New cls_config_parametro_
                obj_config_parametro = obj_config_parametro.Buscar(nombre)
            End If
            config_parametro = obj_config_parametro
        End Get
        Set(ByVal value As cls_config_parametro_)
            obj_config_parametro = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_config_parametrousuario_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_config_parametrousuario_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_config_parametrousuario_)
        While obj_Reader.Read()
            Dim Elemento As New cls_config_parametrousuario_
            Elemento.int_idparametrousuario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idparametrousuario")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idparametro"))) = False Then
                Elemento.str_idparametro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idparametro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idusuario"))) = False Then
                Elemento.int_idusuario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idusuario")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
                Elemento.str_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idparametrousuario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idparametrousuario")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idparametro"))) = False Then
            str_idparametro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idparametro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idusuario"))) = False Then
            int_idusuario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idusuario")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("valor"))) = False Then
            str_valor = (obj_Reader.GetValue(obj_Reader.GetOrdinal("valor")))
        End If
    End Sub

    Public Function Nuevo(Optional ByVal ObtenerID As Boolean = False) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__Nuevo"
                .Parameters.Add("@idparametro", SqlDbType.NVarChar).Value = str_idparametro
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@idusuario", SqlDbType.Int).Value = int_idusuario
                .Parameters.Add("@valor", SqlDbType.NVarChar).Value = str_valor
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Crear el registro de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Function

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__Modificar"
                .Parameters.Add("@idparametrousuario", SqlDbType.Int).Value = int_idparametrousuario
                .Parameters.Add("@idparametro", SqlDbType.NVarChar).Value = str_idparametro
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@idusuario", SqlDbType.Int).Value = int_idusuario
                .Parameters.Add("@valor", SqlDbType.NVarChar).Value = str_valor
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Modificar el registro de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idparametrousuario As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__Eliminar"
                .Parameters.Add("@idparametrousuario", SqlDbType.Int).Value = idparametrousuario
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Eliminar el registro de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idparametrousuario As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__Buscar"
                .Parameters.Add("@idparametrousuario", SqlDbType.Int).Value = idparametrousuario
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function BuscarXnombreXidusuario(ByVal prm_nombre As String, ByVal prm_idusuario As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            BuscarXnombreXidusuario = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__BuscarXnombreXidusuario"
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = prm_nombre
                .Parameters.Add("@idusuario", SqlDbType.Int).Value = prm_idusuario
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                BuscarXnombreXidusuario = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al Buscar el registro de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarUltimo registro de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "config_parametrousuario__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            Throw New Exception("Error al CargarTodos los registros de config_parametrousuario_." & Chr(13) & _
           "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
           "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
           "Descripción: " & ex.Message)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_config_parametrousuario_Clon As New cls_config_parametrousuario_
        obj_config_parametrousuario_Clon.int_idparametrousuario = Me.int_idparametrousuario
        obj_config_parametrousuario_Clon.str_idparametro = Me.str_idparametro
        obj_config_parametrousuario_Clon.str_nombre = Me.str_nombre
        obj_config_parametrousuario_Clon.int_idusuario = Me.int_idusuario
        obj_config_parametrousuario_Clon.str_valor = Me.str_valor
        obj_config_parametrousuario_Clon.col_Items = Me.col_Items
        Return obj_config_parametrousuario_Clon
    End Function

End Class
